/**
 * 
 */
package verma.abhishek;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Formatter;
import java.util.Properties;

import different.pkg.Child;

/**
 * @author Abhishek
 * 
 */
public class RunMe {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(">>> Test 1 - Calling constructor");
		Child c = new Child();
		// c.method1() ; - method is not visible

		System.out.println(">>> Test 2 - Calling formatter");
		formatter();

		System.out.println(">>> Test 3 - String check");
		stringCheck();

		System.out.println(">>> Test 4 - Number evaluation");
		evaluation();

		System.out.println(">>> Test 5 - Ports available (Blocked)");
		// socketAvailable();

		System.out.println(">>> Test 6 - Testing Wrapper types");
		testingWrapperTypes();
		
		System.out.println (">>> Test 7 - Tricky String") ;
		trickyString() ;

		System.out.println(">>> END");
	}

	private static void formatter() {
		Formatter formatter = new Formatter();
		formatter.format("%tT", Calendar.getInstance());

		System.out.println("Local time: " + formatter);
	}

	private static void stringCheck() {
		String s1 = "HELLO";
		String s2 = new String("HELLO");

		System.out.println("s1 === s2:" + (s1 == s2)); // false
		System.out.println("s1.toString() == s2.toString():"
				+ (s1.toString() == s2.toString())); // false
		System.out.println("s1.equals(s2): " + (s1.equals(s2))); // true
		System.out.println("s1.hashCode() == s2.hashCode(): "
				+ (s1.hashCode() == s2.hashCode())); // true
	}
	
	private static void trickyString () {
		String s1 = "HELLO" + "WORLD" ;
		String s2 = "HELLO" ;
		String s3 = "WORLD" ;
		String s4 = s2+s3 ;
		
		System.out.println ("s4.equals(s1): "+s4.equals(s1)) ; // true
		System.out.println ("s4 == s1: "+(s4==s1)) ; // false
	}

	private static void evaluation() {
		int i = 5;

		System.out.println("i^0: " + (i ^ 0));
	}

	private static void socketAvailable() {
		for (int i = 1; i < 1024; i++) {
			try {
				System.out.print(".");
				Socket s = new Socket("localhost", i);
				System.out.println("An Application is running on port number "
						+ i);
			} catch (UnknownHostException ex) {
				break;
			} catch (IOException ex) {
			}
		}
	}

	private static void readingPropertiesFile() throws FileNotFoundException,
			IOException {
		Properties p = new Properties();
		p.load(new FileInputStream("prop.properties"));
		/**
		 * The two lines below return compilation error as getProperty returns a
		 * String object.
		 */
		// int x = p.getProperty("x");
		// int y = p.getProperty("y");
		// System.out.println(x * y);
	}

	private static void testingWrapperTypes() {
		Integer i = new Integer(10);
		Long l = new Long(100L);

		System.out.println("i.equals(l): " + (i.equals(l))); // return false
	}

	private static void trickyIfStatement () {
        int aNumber=0;
        if (aNumber >= 0);
            if (aNumber == 0);
            System.out.print(" 1 ");
        // The line below gives a compilation error
            // else System.out.print(" 2 ");
        System.out.print(" 3 ");
	}
	
	private static void doesThisCompile () {
		int age;
		// The line below gives a compilation error
		// age = age + 1;
		// System.out.println("The age is " + age);
	}
	
} // end of class
